﻿using EkStudio.com.bean.xml.reflection;
using System;
using System.Collections.Generic;

namespace EkStudio.com.bean.xml.RootInterface.Plugin
{
    public class BillExpandJavaCodeRoot : BillUiFormatContainer
    {
        private int maxid = 0;
        private List<BillExpandJavaCodeItem> javaItem = null;

        [Xml("maxid")]
        public int Maxid
        {
            get
            {
                return maxid;
            }

            set
            {
                maxid = value;
            }
        }
        [XmlElement("javaItem")]
        public List<BillExpandJavaCodeItem> JavaItem
        {
            get
            {
                return javaItem;
            }

            set
            {
                javaItem = value;
            }
        }
        [XmlTransient]
        public object ParentBean
        {
            get
            {
                return null;
            }

            set
            {
                
            }
        }

        public object Clone()
        {
            BillExpandJavaCodeRoot clone = new BillExpandJavaCodeRoot();
            clone.Maxid = 0;
            List<BillExpandJavaCodeItem> list = new List<BillExpandJavaCodeItem>();
            if (this.javaItem != null && this.javaItem.Count > 0)
            {
                for (int i = 0; i < javaItem.Count; i++)
                {
                    Object it = javaItem[i].Clone();
                    if (it is BillExpandJavaCodeItem)
                    {
                        list.Add((BillExpandJavaCodeItem)it);
                        clone.Maxid = clone.Maxid + 1;
                    }
                }
            }
            clone.JavaItem = list;
            return clone;
        }

        #region 接口实现
        public void addNode(BillUiFormatNode node, BillFormatModule format)
        {
            if(node is BillExpandJavaCodeItem)
            {
                if (this.javaItem == null) this.javaItem = new List<BillExpandJavaCodeItem>();
                BillExpandJavaCodeItem java = node as BillExpandJavaCodeItem;
                java.LanguageTexts = this.getNLanguageText(format);
                this.javaItem.Add(java);
            }

        }

        public void removeNode(BillUiFormatNode node)
        {
            if(node is BillExpandJavaCodeItem)
            {
                BillExpandJavaCodeItem java = node as BillExpandJavaCodeItem;
                if (this.javaItem.Contains(java))
                {
                    this.javaItem.Remove(java);
                }
            }
        }

        public void moveUp(BillUiFormatNode node)
        {
            throw new NotImplementedException();
        }

        public void moveDown(BillUiFormatNode node)
        {
            throw new NotImplementedException();
        }

        public string getNLanguageText(BillFormatModule format)
        {
            String result = "";
            String language = format.getLanguageText();
            if (language.Length > 0)
            {
                int index = language.IndexOf("Caption");
                if (index != -1)
                {
                    int length = language.Length - 7;
                    String first = language.Substring(0, length);
                    int max = this.maxid + 1;
                    this.maxid = max;
                    result = first + "Java[" + max + "].Caption";
                }
            }
            return result;
        }

        public void updateLanguageText(BillFormatModule format)
        {
            if (this.javaItem != null && this.javaItem.Count > 0)
            {
                foreach(BillExpandJavaCodeItem java in this.javaItem)
                {
                    java.LanguageTexts = this.getNLanguageText(format);
                }
            }
        }

        #endregion
    }
}
